﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Twitterizer;
using haber.classes;

namespace haber
{
    public partial class twitFuncs : System.Web.UI.Page
    {
        public class twitReturn
        {
            public string screenName { get; set; }
            public string description { get; set; }
            public long takipciSayisi { get; set; }
            public long arkadasSayisi { get; set; }
            public long twitSayisi { get; set; }
            public string profilResmi { get; set; }

        }
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        
        [WebMethod]
        public static twitReturn twitBilgileriGetir(int kategoriID)
        {
            using (dcHaberDataContext dc = new dcHaberDataContext())
            {
                var kat = from k in dc.HaberKategorileris
                          where k.KategoriID == kategoriID
                          select k;
                HaberKategorileri hk = kat.Single();

                twitterOauth to = new twitterOauth();
                OAuthTokens tokens = new OAuthTokens();
                tokens.ConsumerKey = to.ConsumerKey;
                tokens.ConsumerSecret = to.ConsumerSecret;
                tokens.AccessToken = hk.TwitterAccessToken;
                tokens.AccessTokenSecret = hk.TwitterAccessTokenSecret;
                TwitterResponse<TwitterUser> twitUser = TwitterUser.Show(tokens, hk.TwitterScreenName);
                if (twitUser.Result == RequestResult.Success)
                {
                    
                        return new twitReturn{arkadasSayisi = twitUser.ResponseObject.NumberOfFriends,
                        description =twitUser.ResponseObject.Description,
                        profilResmi = twitUser.ResponseObject.ProfileImageLocation,
                        screenName = twitUser.ResponseObject.ScreenName,
                        takipciSayisi = twitUser.ResponseObject.NumberOfFollowers.HasValue?twitUser.ResponseObject.NumberOfFollowers.Value:0,
                        twitSayisi  = twitUser.ResponseObject.NumberOfStatuses};
                        
                }
                else
                {
                    // twitUser.ErrorMessage;
                    return null;
                }

            }

            

        }
    }
}